#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;

namespace AbstractClass.Linq
{
    /// <summary>
    ///  Provides a set of static methods for querying <see cref="IDictionary{TKey,TValue}"/>.
    /// </summary>
    public static class DictionaryExtensions
    {
        /// <summary>
        ///   Gets the list of keys associated with <paramref name = "value" />.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="value">The value whose associated keys are to be retrieved.</param>
        /// <returns>A list of keys associated with the <paramref name="value"/>.</returns>
        public static List<TKey> GetKeys<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue value)
        {
            return (from item in dictionary where item.Value.Equals(value) select item.Key).ToList();
        }

        /// <summary>
        /// Adds a list of <see cref="KeyValuePair&lt;TKey, TValue&gt;"/> to the <paramref name = "dictionary" />.
        /// </summary>
        /// <typeparam name="TKey">The type of the keys.</typeparam>
        /// <typeparam name="TValue">The type of the values.</typeparam>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="keyValuePairs">The list of key value pairs to add.</param>
        public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> dictionary,
                                                  IList<KeyValuePair<TKey, TValue>> keyValuePairs)
        {
            if (dictionary == null) throw new ArgumentNullException("dictionary");
            foreach (var keyValuePair in keyValuePairs)
            {
                dictionary.Add(keyValuePair);
            }
        }
    }
}